package com.xiaoye.qbproject.controller.base;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoye.qbproject.constant.Constants;
import com.xiaoye.qbproject.domain.AjaxResult;
import com.xiaoye.qbproject.domain.QbUser;
import com.xiaoye.qbproject.service.LoginService;
import com.xiaoye.qbproject.service.QbUserService;
import com.xiaoye.qbproject.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author : ChenYao
 * @version : 1.0
 * @description :
 * @create : 2024-12-26 17:09
 */
@RestController
@RequestMapping()
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping("/login")
    public AjaxResult login(@RequestBody QbUser user) {
        String username = user.getUsername();
        String password = user.getPassword();
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return AjaxResult.error("用户名或密码不能为空");
        }
        // 创建token
        // 生成令牌
        String token = loginService.login(username, password);
        return AjaxResult.success().put(Constants.TOKEN, token);
    }

    @DeleteMapping("/logout")
    public AjaxResult logout(HttpServletRequest request) {
        loginService.logout(request);
        return AjaxResult.success("退出成功");
    }
}
